<?php
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_once 'includes/class.phpmailer.php';

if(isset($_GET['usn']) && isset($_GET['cnc']) && $_GET['usn']!='' && $_GET['cnc']!='')
{
$userName=$_GET['usn'];
$cfmCode=$_GET['cnc'];
$sqlSelUsrTmp="SELECT * FROM tbl_temp_user WHERE 
      	username='$userName' and confirmation_code ='$cfmCode'";
$rstSelUsrTmp=mysql_query($sqlSelUsrTmp);
$noSelUsrTmp=mysql_num_rows($rstSelUsrTmp);
if($noSelUsrTmp>0)
{
$rowSelUsrTmp=mysql_fetch_array($rstSelUsrTmp);
extract($rowSelUsrTmp);
  $sqlSelUsr="SELECT email FROM tbl_users WHERE username='$userName'";
  $rstSelUsr=mysql_query($sqlSelUsr);
  $noSelUsr=mysql_num_rows($rstSelUsr);
  if($noSelUsr==0) 
  {
   $sqlInsUsr="INSERT INTO tbl_users SET   
  				 	username='".$username."',
					password='".$password."',
					user_type=".$user_type.",
					first_name='".$first_name."',
					last_name='".$last_name."',
					address_line_1='".$address_line_1."',
					address_line_2='".$address_line_2."',
					address_line_3='".$address_line_3."',
					city='".$city."',
					state='".$state."',
					country='".$country."',
					zip='".$zip."',
					phone_number_1='".$phone_number_1."',
					email='".$email."'";
				
					
	if($user_type=='1' || $user_type=='2')
      $sqlInsUsr=$sqlInsUsr.", status=1 , secret_question='".$secret_question."',province='".$province."',secret_answer='".$secret_answer."',dob='".$dob."',created_date='".$created_date."'";
  else if($user_type=='3')
    $sqlInsUsr=$sqlInsUsr.", status=2 ,bus_id='".$bus_id."',secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',created_date='".$created_date."'";
  else if($user_type=='4')
    $sqlInsUsr=$sqlInsUsr.", status=4 ,secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',created_date='".$created_date."'";
	else if($user_type=='5')
	$sqlInsUsr=$sqlInsUsr.", status=2 ,secret_question='".$secret_question."',secret_answer='".$secret_answer."',business_name='".$business_name."' ,phone_number_2='".$phone_number_2."',special_in='".$special_in."'";
	$rstInsUsr = mysql_query($sqlInsUsr)or die(mysql_error());
	$insertedId = mysql_insert_id(); 
	
	$sqlSelItem="select * from tbl_referer where ref_email like '%".$email."%'";
	$rstSelItem=mysql_query($sqlSelItem);
	$rowSelItem=mysql_fetch_array($rstSelItem);	
	if(is_array($rowSelItem))
	{
		$sqlPost="update tbl_referer set refer_to_user_id='".$insertedId."',isActive='1' where ref_email like '%".$email."%'";
		//echo $sqlPost;
		$rstPost=mysql_query($sqlPost) or die(mysql_error());		
	}
	
	if($insertedId>0)
	{
	
	if($user_type=="3" || $user_type=="5")
	{
	 $sql="select count(*) as cnt from tbl_users where status=4 AND (user_type=3 OR user_type=5) AND (business_name like '%$businessName%' OR address_line_1 LIKE '%$address1' OR email='$email' OR first_name like '%$firstName%' OR last_name like '%$lastName%'  OR dob='$dob')";
	 $rs=mysql_query($sql);
	 $row=mysql_fetch_array($rs);
	 $cnt=$row['cnt'];
	 
	
	if($cnt>0)
	  {
		    $msg1="<table width='100%' border='0'>
		<tr>
		  <td width='71%' height='124'><p><b>Hi Support team, </b>, </p>
		  <p>Following user's registration info is matching to user which is closed by swapen Admin.</p></td>
		  <td width='29%' style='text-align:center'>
		      <b>Username:</b>".$username."<br/>
		      <b>Email:</b>".$email."<br/>
		      <b>First name:</b>".$first_name."<br/>
		      <b>Last name:</b>".$last_name."<br/>
		  </td>
		</tr>
	      </table>";
	      $email1="Support@swapen.com";
	      $mail = new PHPMailer(); // defaults to using php "mail()"
			$mail->MsgHTML($msg1);
			$mail->From       = "mail@swapen.com";
			$mail->FromName   = "Swapen.com";
			$mail->Subject    = "Merchant/Whole seller Acoount info. matching to closed user";
			$mail->AddAddress($email1);
			$mail->Send();
	  }
	}
	
	if($user_type==1 || $user_type==2)
	{
	   $sql="select count(*) as cnt from tbl_users where status=4 AND (user_type=1 OR user_type=2) AND (first_name like '%$firstName%' OR last_name like '%$lastName%'  OR dob='$dob')";
	   $rs=mysql_query($sql);
	   $row=mysql_fetch_array($rs);
	   $cnt=$row['cnt'];
	
	  if($cnt>0)
	   {
	      $sql=mysql_query("update tbl_users set status='3' where id=$insertedId");
	      mysql_query($sql);
	   }
	}
	 $sqlEmail="select * from tbl_emails where email_type='Confirmation'";
	 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
	  $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
  <tr>
    <td width='18%' height='41'><table width='100%' border='0'>
      <tr>
        <td width='19%' height='68'><img src='http://www.swapen.com/images/logo.gif'></td>
        <td width='81%'> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$rowEmail['email_body']."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = $rowEmail['email_subject'];
		$mail->AddAddress($email, $firstName);
		$mail->Send();
	 if($user_type==3)
	 {
	
	 $msg="<font face=verdana size=2 color='#005B00'><b>There is a 1-3 business day wait for verification purposes, but you can start creating your inventory list today! Once you are verified as a Swapen Merchant, your inventory list will go online!</b></font>";
	
	 }
	 else if($user_type==5)
	 {
	 $msg="<font face=verdana size=2 color='#005B00'><b>There is a 1-3 business day wait for verification purposes, but you can start creating your inventory list today! Once you are verified as a Swapen Wholeseller, your inventory list will go online!</b></font>";
	 }
	 else
	 {
	 $msg="<font face=verdana size=2 color='#005B00'><b>Your account successfully confirmed, <a href='<?=$base_url?>index.php?signin'>Signin here</a> to continue.</b></font>";
	 }
	}
}
else
    $msg = "<font face=verdana size=2 color='#FF0000'><b>This email id already exist in another account, enter valid one.</b></font>";
}
else
	$msg = "<font face=verdana size=2 color='#FF0000'><b>Invalid confirmation code.</b></font>";
}
else
	$msg = "<font face=verdana size=2 color='#FF0000'><b>Invalid URL.</b></font>";
?>
<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Registration confirmation</h2></td>
  </tr>
  <tr>
    <td class="content"><p><?php if(!empty($msg))
									echo $msg;
	?></p>
    </td>
  </tr>
</table>
</div>